﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Reflection;
using System.Text;

namespace MuXue.Zyiz.Template.Common
{
    public class ConvertHelper
    {
        public static IList<T> DataTableToIList<T>(DataTable dt)
        {
            if (dt == null)
                return null;

            DataTable p_Data = dt;
            // 返回值初始化
            IList<T> result = new List<T>();
            for (int j = 0; j < p_Data.Rows.Count; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {
                        // 属性与字段名称一致的进行赋值
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理
                            if (p_Data.Rows[j][i] != DBNull.Value)
                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else
                                pi.SetValue(_t, null, null);
                            break;
                        }
                    }
                }
                result.Add(_t);
            }
            return result;
        }


        public static T DataTableToEntity<T>(DataTable dt)
        {
            if (dt == null || dt.Rows.Count < 1)
                return default(T);

            DataTable p_Data = dt;
            // 返回值初始化
            // IList<T> result = new List<T>();

            for (int j = 0; j < 1; j++)
            {
                T _t = (T)Activator.CreateInstance(typeof(T));
                PropertyInfo[] propertys = _t.GetType().GetProperties();
                foreach (PropertyInfo pi in propertys)
                {
                    for (int i = 0; i < p_Data.Columns.Count; i++)
                    {
                        // 属性与字段名称一致的进行赋值
                        if (pi.Name.Equals(p_Data.Columns[i].ColumnName))
                        {
                            // 数据库NULL值单独处理
                            if (p_Data.Rows[j][i] != DBNull.Value)
                                pi.SetValue(_t, p_Data.Rows[j][i], null);
                            else
                                pi.SetValue(_t, null, null);
                            break;
                        }
                    }
                }
                // result.Add(_t);
                return _t;
            }
            return default(T);
            // return result;
        }


        /// <summary>
        /// 字符串如果为null，则返回默认值
        /// </summary>
        /// <param name="obj"></param>
        /// <param name="defaultStr"></param>
        /// <returns></returns>
        public static string StrIsNull(string obj,string defaultStr)
        {
            if (obj == null)
            {
                return defaultStr;
            }
            return obj;


        }

        /// <summary>
        /// 字符串转int
        /// </summary>
        /// <param name="str"></param>
        /// <param name="defaultInt"></param>
        /// <returns></returns>
        public static int IntParse(string str, int defaultInt)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return defaultInt;
            }
            int result = 0;
            if (int.TryParse(str, out result))
            {
                return result;
            }
            else
            {
                return defaultInt;
            }

        }


        /// <summary>
        /// 去掉左右空格，如果为null，则返回空字符串
        /// </summary>
        /// <param name="str"></param>
        /// <returns></returns>
        public static string Trim(string str)
        {
            if (string.IsNullOrWhiteSpace(str))
            {
                return "";
            }
            return str.Trim();

        }

    }
}
